import { Dialog, Toast } from 'vant';
import BuyAPI from "../repo/api/BuyAPI";


export function verifyOrder(prepayId) {

  if (!prepayId) {
    return
  }
  Toast.loading({
    duration: 0,
    message: '正在验证订单',
    forbidClick: true,
    loadingType: 'spinner',
  })

  BuyAPI.orderConfirm(prepayId).then((result) => {
    Toast.clear()
    let data = result.responseData
    if (data.status === 1) {
      Dialog.alert({
        title: '提示',
        message: '订单验证成功',
        confirmButtonText: "确定"
      }).then(() => {
        replaceUrl()
      });
    } else {
      Dialog.confirm({
        title: '提示',
        message: '支付失败',
        confirmButtonText: "我已完成付款",
        cancelButtonText: "取消"
      })
        .then(() => {
          verifyOrder(prepayId)
        })
        .catch(() => {
          replaceUrl()
        });
    }
  }).catch((e) => {
    Toast.clear()

    Dialog.confirm({
      title: '提示',
      message: '订单验证失败',
      confirmButtonText: "重新验证",
      cancelButtonText: "取消"
    })
      .then(() => {
        verifyOrder(prepayId)
      })
      .catch(() => {
        replaceUrl()
      });

  })


}
function replaceUrl() {
  history.go(-2)
  // let url = window.location.href
  // let idx = -1
  // if (url.indexOf('?prepayId') > -1) {
  //   idx = url.indexOf('?prepayId')
  // } else if (url.indexOf('&prepayId') > -1) {
  //   idx = url.indexOf('&prepayId')
  // }
  // window.location.replace(url.slice(0, idx))
}
